from pydantic_settings import BaseSettings
from typing import List, Union
import os


class Settings(BaseSettings):
    # 应用配置
    app_name: str = "多模态AI聊天应用"
    debug: bool = True
    secret_key: str = "your-secret-key-here-change-in-production"
    access_token_expire_minutes: int = 30
    
    # 数据库配置
    database_url: str = "sqlite:///./chat.db"
    
    # Redis配置
    redis_url: str = "redis://localhost:6379"
    redis_password: str = ""
    
    # AI模型配置
    openai_api_base: str = "https://api.example.com/v1"
    openai_api_key: str = "your-openai-api-key"
    openai_model: str = "Qwen/Qwen3-8B"
    
    ocr_api_base: str = "https://api.example.com/v1"
    ocr_api_key: str = "your-ocr-api-key"
    ocr_model: str = "deepseek-ai/DeepSeek-OCR"
    
    # Tavily搜索工具配置
    tavily_api_key: str = "your-tavily-api-key"
    tavily_search_enabled: bool = True  # 从环境变量读取，默认启用搜索
    tavily_max_results: int = 3  # 搜索返回的最大结果条数
    
    # 文件上传配置
    max_file_size: int = 5242880  # 5MB
    allowed_extensions: List[str] = ["txt", "pdf", "doc", "docx", "jpg", "jpeg", "png", "gif"]
    
    # CORS配置 - 使用Union类型接受字符串或列表
    allowed_origins: Union[str, List[str]] = "http://localhost:3000,http://127.0.0.1:3000"
    
    def get_allowed_origins(self) -> List[str]:
        """解析环境变量中的CORS配置"""
        if isinstance(self.allowed_origins, str):
            # 如果是字符串，按逗号分割
            return [origin.strip() for origin in self.allowed_origins.split(",") if origin.strip()]
        elif isinstance(self.allowed_origins, list):
            return self.allowed_origins
        else:
            return ["http://localhost:3000", "http://127.0.0.1:3000"]
    
    class Config:
        env_file = "../.env"


settings = Settings()